/**
* 左边菜单
*/
<template>
  <el-menu
    default-active="2"
    :collapse="collapsed"
    collapse-transition
    router
    unique-opened
    class="el-menu-vertical-demo"
    background-color="#334157"
    text-color="#fff"
    active-text-color="#ffd04b"
  >
    <div class="logobox">
      <img class="logoimg" src="../assets/img/logo.png" alt="" />
    </div>
    <el-submenu v-for="menu in allmenu" :key="menu.id" :index="menu.menuname">
      <!-- 一级菜单-->
      <template slot="title">
        <i class="iconfont" :class="menu.icon"></i>
        <span>{{ menu.menuname }}</span>
      </template>


      <!-- 二级菜单-->
          <el-submenu v-for="chmenu in menu.menus" v-if="chmenu.menus.length>0" :index="'/' + chmenu.url" :key="chmenu.id">
          <template slot="title">
            <i class="iconfont" :class="chmenu.icon"></i>
            <span>{{ chmenu.menuname }}</span>
          </template> 

          <!--三级菜单-->
          <el-menu-item-group>
            <el-menu-item
              v-for="threemenu in chmenu.menus"
              :index="'/' + threemenu.url"
              :key="threemenu.id"
            >
              <i class="iconfont" :class="threemenu.icon"></i>
              <span>{{ threemenu.menuname }}</span>
            </el-menu-item>
          </el-menu-item-group>

       </el-submenu> 


       <!-- 判断二级菜单是否有子菜单-->
       <el-menu-item
         v-for="chmenu1 in menu.menus"
         v-if="chmenu1.menus.length==0"
         :index="'/' + chmenu1.url"
         :key="chmenu1.id"
       >
         <i class="iconfont" :class="chmenu1.icon"></i>
         <span>{{ chmenu1.menuname }}</span>
       </el-menu-item>

    </el-submenu>
  </el-menu>
</template>
<script>
import axios from "axios";
export default {
  name: "leftnav",
  data() {
    return {
      collapsed: false,
      allmenu: [],
    };
  },
  // 创建完毕状态(里面是操作)
  created() {
    // 获取图形验证码
    let res = {
      success: true,
      data: [
        {
          menuid:2,
          icon: "icon-cat-skuQuery",
          menuname:"课程管理",
          hasThird: null,
          url: null,
        },
        {
          menuid: 1,
          icon: "li-icon-xiangmuguanli",
          menuname: "考试管理",
          hasThird: null,
          url: null,
          menus: [
            {
              menuid: 2,
              icon: "icon-cat-skuQuery",
              menuname: "试题题库",
              hasThird: "N",
              url: "exam/question",
              menus: [
                {
                  menuid: 2,
                  icon: "icon-cat-skuQuery",
                  menuname: "题库列表",
                  hasThird: "N",
                  url:"exam/question/banklist",
				  menus:null

                },{
                  menuid: 2,
                  icon: "icon-cat-skuQuery",
                  menuname: "试题添加界面",
                  hasThird: "N",
                  url:"exam/question/addquestion",
				  menus:null
                },{
                  menuid: 2,
                  icon: "icon-cat-skuQuery",
                  menuname: "试题列表",
                  hasThird: "N",
                  url:"exam/question/tag",
				  menus:null
                },
              ],
            },
            {
              menuid: 3,
              icon: "icon-cat-skuQuery",
              menuname: "试卷管理",
              hasThird: "N",
              url: "exam/paper",
              menus: [
                {
                  menuid: 2,
                  icon: "icon-cat-skuQuery",
                  menuname: "试卷列表",
                  hasThird: "N",
                  url:"exam/paper/paperlist",
                  menus:null
                },
                {
                  menuid: 2,
                  icon: "icon-cat-skuQuery",
                  menuname: "编辑试卷",
                  hasThird: "N",
                  url:"exam/paper/edit",
                  menus:null
                }
              ],
            },
            {
              menuid: 4,
              icon: "icon-cat-skuQuery",
              menuname: "考试管理",
              hasThird: "N",
              url: "exam/test",
              menus: [
                {
                  menuid: 2,
                  icon: "icon-cat-skuQuery",
                  menuname: "添加考试",
                  hasThird: "N",
                  url:"exam/test/addexam",
				   menus:null
                },{
                  menuid: 2,
                  icon: "icon-cat-skuQuery",
                  menuname: "考试列表",
                  hasThird: "N",
                  url:"exam/test/examlist",
				  menus:null
                },{
                  menuid: 2,
                  icon: "icon-cat-skuQuery",
                  menuname: "考试分类",
                  hasThird: "N",
                  url:"exam/test/examtype",
				  menus:null
                }
              ],
            },
            {
              menuid: 5,
              icon: "icon-cat-skuQuery",
              menuname: "阅卷管理",
              hasThird: "N",
              url: "exam/readexam",
			   menus: null,
            },
          ],
        },
        {
          menuid: 33,
          icon: "li-icon-dingdanguanli",
          menuname: "讲师管理",
          hasThird: null,
          url: null,
		  menus:null
        },
        {
          menuid: 71,
          icon: "li-icon-xitongguanli",
          menuname: "档案管理",
          hasThird: null,
          url: null,
          menus: [
            {
              menuid: 72,
              icon: "icon-cus-manage",
              menuname: "学员考试管理",
              hasThird: "N",
              url: "user/userexam",
			  menus: null,
            },
          ],
        },{
          menuid: 71,
          icon: "li-icon-xitongguanli",
          menuname: "学习地图",
          hasThird: null,
          url: null,
		  menus: null
        }
      ],
      msg: "success",
    };

    this.allmenu = res.data;
    var roleId = JSON.parse(localStorage.getItem("userdata")).roleid;

    //如果有是id为1的管理员
    if (roleId == 1) {
      axios
        .get("http://localhost:8081/menu/findAllMenu")
        .then((res) => {
          this.allmenu = res.data;
          console.log("---------", this.allmenu);
        })
        .catch((err) => {
          this.$message.error("菜单加载失败，请稍后再试！");
        });
    }else{
    axios
      .get("http://localhost:8081/menu/findLeftMenu/" + roleId)
      .then((res) => {
        this.allmenu = res.data;
        console.log("---------", this.allmenu);

      })
      .catch((err) => {
        this.$message.error("菜单加载失败，请稍后再试！");
      });
    }
    // menu(localStorage.getItem('logintoken'))
    //   .then(res => {
    //     console.log(JSON.stringify(res))
    //     if (res.success) {
    //       this.allmenu = res.data
    //     } else {
    //       this.$message.error(res.msg)
    //       return false
    //     }
    //   })
    //   .catch(err => {
    //     this.$message.error('菜单加载失败，请稍后再试！')
    //   })
    // 监听
    this.$root.Bus.$on("toggle", (value) => {
      this.collapsed = !value;
    });
  },
};
</script>
<style>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 240px;
  min-height: 400px;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  border: none;
  text-align: left;
}
.el-menu-item-group__title {
  padding: 0px;
}
.el-menu-bg {
  background-color: #1f2d3d !important;
}
.el-menu {
  border: none;
}
.logobox {
  height: 40px;
  line-height: 40px;
  color: #9d9d9d;
  font-size: 20px;
  text-align: center;
  padding: 20px 0px;
}
.logoimg {
  height: 40px;
}
</style>
